package req

import (
	"strconv"
	"strings"
	"encoding/json"
)

type Request struct {
	params map[string]interface{}
	sort []string  // [0]-sortField  [1] -sortOrder  asc|desc
	pageIndex int
	pageSize int
}

func NewRequest()  *Request {
	cond:=&Request{}
	cond.params=make(map[string]interface{},0)
	cond.sort=make([]string,2)
	cond.pageIndex=1
	cond.pageSize=10
	return cond
}
func (this *Request) SetPageIndex(pageIndex int){
	if pageIndex<=0{
		this.pageIndex=1
	}
	this.pageIndex=pageIndex
}

func (this *Request) SetPageSize(pageSize int){
	if pageSize<=0{
		this.pageSize=10
	}
	this.pageSize=pageSize
}

func (this *Request) SetSort(field string,direct string) {
	this.sort=make([]string,2)
	this.sort[0]=field
	this.sort[1]=direct
}

func (this *Request) SetParams(params map[string]interface{}) {
	if params!=nil && len(params)>0{
		for k,v := range params {

			if k=="pageNo" || k=="pageIndex"{
				i_v,_:=strconv.Atoi(v.(string))
				this.pageIndex=i_v
			}else if k=="pageSize"{

				i_v,_:=strconv.Atoi(v.(string))
				this.pageSize=i_v
			}else if k=="sortField" || k=="sortfield"{
				this.sort[0]=v.(string)
			}else if k=="sortOrder" || k=="sortorder"{
				sortOrder:=v.(string)
				if strings.HasPrefix(sortOrder,"asc"){
					this.sort[1]="asc"
				}else{
					this.sort[1]="desc"
				}

			}else{
				this.AddParams(k,v)
			}

		}
	}
}

func (this *Request) AddParams(key string,value interface{}) {
	this.params[key]=value
}

func (this *Request)  PageIndex() int{
	return this.pageIndex
}
func (this *Request)  PageSize() int{
	return this.pageSize
}

func (this *Request)  Sort() []string{
	return this.sort
}
func (this *Request)  Params() map[string]interface{}{
	return this.params
}

func (this *Request)  GetParamValueInt64(key string) int64{
	v:=this.params[key]

	var i int64
	switch v.(type){
		case int64:
			i=v.(int64)
		case string:
			i,_=strconv.ParseInt(v.(string),10,64)
		case int:
			i,_=strconv.ParseInt(v.(string),10,64)
	    case float64: //整型传递到interface 中 会被转成 float64
		    i,_= strconv.ParseInt(strconv.FormatFloat(v.(float64),'f',-1,64),10,64)
	case json.Number:
		a:=v.(json.Number)
		ia,_:=a.Int64()
		i=ia
	}

	return i
}
func (this *Request)  GetParamValueInt32(key string) int32{
	v:=this.params[key]

	var i int32

	switch v.(type){
	case int64:
		i=v.(int32)
	case int:
		i=int32(v.(int))
	case string:
		i64,_:=strconv.ParseInt(v.(string),10,64)
		i=int32(i64)
	case json.Number:
		a:=v.(json.Number)
		ia,_:=a.Int64()
		i=int32(ia)
	}

	return i
}

func (this *Request)  GetParamValue(key string) interface{}{
	return this.params[key]
}

func (this *Request) GetParamValueString(key string) string {
	if this.params[key]!=nil{
		return this.params[key].(string)
	}else{
		return ""
	}

}


func (this *Request) GetParamValueFloat64(key string) float64 {
	v:=this.params[key]

	if v!=nil{

		var f64 float64
		switch v.(type){
		case string:
			strvalue:=this.params[key].(string)
			f64,_=strconv.ParseFloat(strvalue,64)
		case float64:
			f64=v.(float64)
		case json.Number:
			a:=v.(json.Number)
			ia,_:=a.Float64()
			f64=ia
		}
		return f64
	}else{
		return 0
	}

}